<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/12/21
 * Time: 10:18
 */

namespace Shop\Api;


use Common\Api\Api;
use Shop\Model\CouponTplModel;

class CouponTplApi extends Api {

    /**
     * 启用
     */
    const ENABLE = "Shop/CouponTpl/enable";
    /**
     * 禁用
     */
    const DISABLE = "Shop/CouponTpl/disable";
    /**
     * 添加
     */
    const ADD = "Shop/CouponTpl/add";
    /**
     * 保存
     */
    const SAVE = "Shop/CouponTpl/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "Shop/CouponTpl/saveByID";

    /**
     * 删除
     */
    const DELETE = "Shop/CouponTpl/delete";

    /**
     * 查询
     */
    const QUERY = "Shop/CouponTpl/query";
    /**
     * 查询
     */
    const QUERY_NO_PAGING = "Shop/CouponTpl/queryNoPaging";
    /**
     * 查询一条数据
     */
    const GET_INFO = "Shop/CouponTpl/getInfo";

    protected function _init(){
        $this->model = new CouponTplModel();
    }

    public function query($map = null, $page = array('curpage'=>0,'size'=>10), $order = false, $params = false, $fields = false) {
        $query = $this->model;
        if(!is_null($map)){
            $query = $query->where($map);
        }
        if(!($order === false)){
            $query = $query->order($order);
        }
        if(!($fields === false)){
            $query = $query->field($fields);
        }

        $list = $query ->alias(" cou ")
            ->field('cou.id as id,dt.name as dtree_type_name,cou.dtree_type,cou.use_condition,cou.status,cou.expire_time,cou.money,cou.notes')
            ->join('LEFT JOIN common_datatree as dt on dt.id = cou.dtree_type')
            -> page($page['curpage'] . ',' . $page['size']) -> select();


        if ($list === false) {
            $error = $this -> model -> getDbError();
            return $this -> apiReturnErr($error);
        }

        $count = $this -> model -> where($map) -> count();
        // 查询满足要求的总记录数
        $Page = new \Think\Page($count, $page['size']);

        //分页跳转的时候保证查询条件
        if ($params !== false) {
            foreach ($params as $key => $val) {
                $Page -> parameter[$key] = urlencode($val);
            }
        }

        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page -> show();

        return $this -> apiReturnSuc(array("show" => $show, "list" => $list));
    }

}